package org.agent.aigccore.model.po;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 操作日志实体类
 * 超大宽表设计，支持多种类型日志
 */
@Data
public class OpsLogPO implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 使用静态ObjectMapper以避免频繁创建实例
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    
    private Long id;
    private OffsetDateTime ts; // 使用OffsetDateTime存储UTC+8时间
    
    // 日志类型：OPERATION(操作日志)、SCHEDULE(调度日志)、DEVICE(设备日志)、SYSTEM(系统日志)
    private String logType;
    private String level;
    private String app;
    private String module;
    private String action;
    private String tenantId;
    private String userId;
    private String requestId;
    private String sessionId;
    private String ip; // store as string text of INET
    private String method;
    private String path;
    private Integer statusCode;
    private Boolean success;
    private Integer durationMs;
    private String errorCode;
    private String message;
    private String stack;
    
    // 存储JSONB数据的字段
    private String extra; // 数据库中对应JSONB类型
    
    // IoT调度相关扩展字段
    private String schedulerId; // 调度器ID
    private String robotId; // 机器人ID
    private String deviceId; // 设备ID
    private String deviceType; // 设备类型
    private String taskId; // 任务ID
    private String taskType; // 任务类型
    
    // 设备数据相关字段
    private String deviceData; // 数据库中对应JSONB类型，设备上报数据
    
    /**
     * 获取extra字段的Map表示
     * @return Map对象
     */
    public Map<String, Object> getExtraMap() {
        if (extra == null || extra.isEmpty()) {
            return new HashMap<>();
        }
        try {
            return OBJECT_MAPPER.readValue(extra, Map.class);
        } catch (IOException e) {
            return new HashMap<>();
        }
    }
    
    /**
     * 设置extra字段为Map对象
     * @param extraMap Map对象
     */
    public void setExtraMap(Map<String, Object> extraMap) {
        if (extraMap == null || extraMap.isEmpty()) {
            this.extra = "{}";
        } else {
            try {
                this.extra = OBJECT_MAPPER.writeValueAsString(extraMap);
            } catch (IOException e) {
                this.extra = "{}";
            }
        }
    }
    
    /**
     * 获取deviceData字段的Map表示
     * @return Map对象
     */
    public Map<String, Object> getDeviceDataMap() {
        if (deviceData == null || deviceData.isEmpty()) {
            return new HashMap<>();
        }
        try {
            return OBJECT_MAPPER.readValue(deviceData, Map.class);
        } catch (IOException e) {
            return new HashMap<>();
        }
    }
    
    /**
     * 设置deviceData字段为Map对象
     * @param deviceDataMap Map对象
     */
    public void setDeviceDataMap(Map<String, Object> deviceDataMap) {
        if (deviceDataMap == null || deviceDataMap.isEmpty()) {
            this.deviceData = "{}";
        } else {
            try {
                this.deviceData = OBJECT_MAPPER.writeValueAsString(deviceDataMap);
            } catch (IOException e) {
                this.deviceData = "{}";
            }
        }
    }
    
    /**
     * 获取extra字段的JsonNode表示
     * @return JsonNode对象
     */
    public JsonNode getExtraJson() {
        if (extra == null || extra.isEmpty()) {
            try {
                return OBJECT_MAPPER.readTree("{}");
            } catch (IOException e) {
                return null;
            }
        }
        try {
            return OBJECT_MAPPER.readTree(extra);
        } catch (IOException e) {
            return null;
        }
    }
    
    /**
     * 获取deviceData字段的JsonNode表示
     * @return JsonNode对象
     */
    public JsonNode getDeviceDataJson() {
        if (deviceData == null || deviceData.isEmpty()) {
            try {
                return OBJECT_MAPPER.readTree("{}");
            } catch (IOException e) {
                return null;
            }
        }
        try {
            return OBJECT_MAPPER.readTree(deviceData);
        } catch (IOException e) {
            return null;
        }
    }
}


